package com.souche.sts.study;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @program: sts
 *
 * @description: Condition
 *
 * @author: heqiang
 *
 * @create: 2019-09-26 14:50
 **/
public class ConditionUseCase {

    Lock lock  = new ReentrantLock();
    Condition condition = lock.newCondition();

    /**
     * 等待后释放锁
     * @throws InterruptedException
     */
    public void conditionWait() throws InterruptedException {
        lock.lock();
        try {
            condition.await();
        } finally {
            lock.unlock();
        }
    }

    /**
     * 通知当前线程，当前线程才从await返回，返回前已经获取了锁。
     * @throws InterruptedException
     */
    public void conditionSignal() throws InterruptedException {
        lock.lock();
        try {
            condition.signal();
        } finally {
            lock.unlock();
        }
    }
}
